<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    v-show="addState"
                    @click="opAdd"
                >新增</el-button>
                <el-button
                    type="info"
                    plain
                    icon="el-icon-delete"
                    v-show="deleteState"
                    @click="opBatchDelete"
                >删除</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px;margin-right: 20px;">
                    <el-row type="flex" style="flex-wrap:wrap" :gutter="20">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="协议编号" label-width="100px" style="width: 100%">
                            <el-input
                                placeholder="---请输入协议编号---"
                                v-model="queryList.purchaseProtocolCode"
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="协议名称" label-width="100px" style="width: 100%">
                            <el-input
                                placeholder="---请输入协议名称---"
                                v-model="queryList.purchaseProtocolName"
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="采购单位" label-width="100px" style="width: 100%">
        
                            <sc-auto-complete
                            :orgType="1"
                            :queryType="'purchase-unity/search'"
                            :placeholder="'---请输入采购单位---'"
                            v-model="queryList.purchaseUnityName"
                            @handleSelect="(e)=>{this.queryList.purchaseUnityName=e.purchaseUnityName}"
                            style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="协议有效期" label-width="100px" style="width: 100%">
                            <el-date-picker
                            style="width: 100%"
                                v-model="queryList.protocolDates"
                                type="datetimerange"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                format="YYYY-MM-DD HH:mm:ss"
                                range-separator="-"
                                unlink-panels="true"
                                start-placeholder="开始时间"
                                end-placeholder="结束时间"
                                :default-time="defaultTime"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            
                            <el-form-item label="物料编码" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入物料编码---"
                                    v-model="queryList.materialCode"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="物料名称" label-width="100px" style="width: 100%">
                            <el-input
                                placeholder="---请输入物料名称---"
                                v-model="queryList.materialName"
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            
                        <el-form-item label="物料分类" label-width="100px" style="width: 100%">
                            <el-tree-select
                                placeholder="---请选择物料分类---"
                                collapse-tags
                                collapse-tags-tooltip
                                v-model="queryList.materialTypeIds"
                                :props="{ label: 'label', value:'value',children: 'children' }"
                                :data="materialTypeTree"
                                multiple
                                :render-after-expand="false"
                                show-checkbox
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="创建时间" label-width="100px" style="width: 100%">
                            <el-date-picker
                                style="width: 100%"
                                v-model="queryList.crDates"
                                type="datetimerange"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                format="YYYY-MM-DD HH:mm:ss"
                                range-separator="-"
                                unlink-panels="true"
                                start-placeholder="开始时间"
                                end-placeholder="结束时间"
                                :default-time="defaultTime"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="创建人" label-width="100px" style="width: 100%">
                            <el-input
                                placeholder="---请输入创建人---"
                                v-model="queryList.crName"
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="最后修改人" label-width="100px" style="width: 100%">
                            <el-input
                                placeholder="---请输入最后修改人---"
                                v-model="queryList.edName"
                                style="width: 100%"
                            />
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="供应商" label-width="100px" v-if="orgType==1" style="width: 100%">
                        
                        <sc-auto-complete
                            :orgType="3"
                            :queryType="'supplier/search'"
                            :placeholder="'---请输入供应商名称---'"
                            v-model="queryList.supplierName"
                            @handleSelect="(e)=>{this.queryList.supplierName=e.supplierName}"
                            style="width: 100%"
                        />
                    </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            
                            <el-form-item label="最后修改时间" label-width="100px" style="width: 100%">
                                <el-date-picker
                                style="width: 100%"
                                    v-model="queryList.edDates"
                                    type="datetimerange"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    format="YYYY-MM-DD HH:mm:ss"
                                    range-separator="-"
                                    unlink-panels="true"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    :default-time="defaultTime"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="状态" label-width="100px" style="width: 100%">
                            <el-select
                                placeholder="---请选择状态---"
                                v-model="queryList.status"
                                style="width: 100%"
                            >
                                <el-option label="---请选择---" value />
                                <el-option
                                    v-for="item in optionsState"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            
                            <el-form-item label="是否调价" label-width="100px" style="width: 100%">
                                <el-select
                                    placeholder="---请选择---"
                                    v-model="queryList.isAdjustPrice"
                                    style="width: 100%"
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option label="是" value=1 />
                                    <el-option label="否" value=0 />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    
                    
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'purchase-protocol/pages'"
                :data="listData"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                :row-class-name="setRowClass"
                row-key="purchaseProtocolId"
                key="purchaseProtocolId"
                show-summary
                remoteSummary
            >
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="协议编号" prop="purchaseProtocolCode" align="center" width="200" sortable>
                    <template #default="scope"> <span class="el-span-text" @click="opShow(scope.row, scope.$index)">{{scope.row.purchaseProtocolCode}}</span></template>
                </el-table-column>
                <el-table-column label="协议名称" prop="purchaseProtocolName" align="center" width="200" sortable></el-table-column>
                <el-table-column label="协议有效期" prop="purchaseProtocolDate" align="center" width="200" sortable></el-table-column>
                <el-table-column label="供应商名称" prop="supplierName" align="center" width="180" sortable></el-table-column>
                <el-table-column label="采购单位" prop="purchaseUnityName" align="center" width="200" sortable></el-table-column>
                <el-table-column label="备注" prop="remarks" align="center" width="200" sortable></el-table-column>
                <el-table-column label="是否调价" prop="isAdjustPriceDesc" align="center" width="105" sortable></el-table-column>
                <el-table-column label="创建人" prop="createName" align="center" width="200" sortable></el-table-column>
                <el-table-column label="创建时间" prop="createTime" align="center" width="200" sortable>
                    <template #default="scope">{{scope.row.createTime?.slice(0,10)}}</template>
                </el-table-column>
                <el-table-column label="最后修改人" prop="updateName" align="center" width="200" sortable></el-table-column>
                <el-table-column label="最后修改时间" prop="updateTime" align="center" width="200" sortable>
                    <template #default="scope">{{scope.row.updateTime?.slice(0,10)}}</template>
                </el-table-column>
                <el-table-column label="状态" fixed="right" align="center" width="120" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="setStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="left" width="240">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="editState"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="addState"
                                @click="opCopy(scope.row, scope.$index)"
                            >复制</el-button>
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        v-show="deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>
</template>

<script>
import saveDialog from "./save";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "purchaseProtocol",
    components: {
        saveDialog,
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["purchaseProtocolId"],
            columnList: [
            ],
            OrderList: { purchaseProtocolId: "Asc" },
            queryList: {},
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            supplierProps: {
                label: "supplierName",
                value: "supplierId",
            },

            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            materialTypeTree: [],
            orgType:3,
            defaultTime:[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ]
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //复制
        opCopy(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add").setData({...row,purchaseProtocolCode:""});
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0
                        ? "purchase-protocol/disabled"
                        : "purchase-protocol/enable",
                    { purchaseProtocolIds: [row.purchaseProtocolId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "purchase-protocol/delete",
                    { purchaseProtocolIds: [row.purchaseProtocolId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //批量删除
        async opBatchDelete() {
            // eslint-disable-next-line no-debugger
            //debugger;
            this.$confirm(
                `确定删除选中的 ${this.selectionRows.length} 项吗？`,
                "提示",
                {
                    type: "warning",
                }
            )
                .then(async () => {
                    const purchaseProtocolIds = this.selectionRows.map(
                        (p) => p.purchaseProtocolId
                    );
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "purchase-protocol/delete",
                                { purchaseProtocolIds: [...purchaseProtocolIds] }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$refs.mytable.getData();
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                })
                .catch(() => {});
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        setRowClass({ row }) {
            if (this.selectionRows.includes(row)) {
                return "selected-row-color";
            }
            return "";
    },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "material-type/tree",
                    { status: 1 }
                );
                if (returnValue.status == 200) {
                    this.materialTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        async orgSelectProcess(rowData) {
            this.queryList.suppliers = [];
            console.log("选择",rowData);
            this.queryList.suppliers = {
                supplierId: rowData.supplierId,
                supplierName: rowData.supplierName,
            };
            this.queryList.supplierId=rowData.supplierId
            
            await this.opBindData();
        },
    },
    async mounted() {
        this.orgType = tool.data.get("USER_INFO").AccountType;
        if (this.orgType != "1") {
            this.addState = false; //可以增加
            this.editState = false; //可以修改
            this.editBatchState = false; //可以批量修改
            this.deleteState = false; //可以删除
        }
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
<style lang="scss" scoped>
/* 当表格行处于悬停状态时改变背景颜色 */
::v-deep .el-table .el-table__row:hover {
    background-color: #dae8f7;
}
::v-deep .el-table__body .el-table__row--striped.hover-row td {
    background-color: #dae8f7 !important;
}
//   斑马纹选中颜色
::v-deep .el-table__body .el-table__row--striped.selected-row-color td {
    background-color: #b8dcff !important;
}
::v-deep .el-table {
    --el-table-row-hover-bg-color: #dae8f7;
}

::v-deep .el-table tr:nth-child(2n) {
    background: #f2f2f2;
}
</style>
<style>
.el-table .selected-row-color {
    background-color: #b8dcff !important;
}
.el-table--enable-row-hover .el-table__body tr.selected-row-color:hover > td {
    background-color: #b8dcff !important;
}
.el-table__body-wrapper .el-table__body tr.selected-row-color.hover-row > td {
    background-color: #b8dcff !important;
}
.el-table__fixed-body-wrapper
    .el-table__body
    tr.selected-row-color.hover-row
    > td {
    background-color: #b8dcff !important;
}
.el-table__body-wrapper
    .el-table__body
    tr.selected-row-color.hover-row
    > .el-table__row--striped {
    background-color: #b8dcff !important;
}
</style>